Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Apr 20, 2025

Closes #114744

static void Test(int count)
{
    int[] arrA = new int[count];
    float[] arrB = new float[count];
    for (int i = 0; i < count; i++)
    {
        arrA[i] = 0;     // bounds check
        arrB[i] = 0.0f;  // bounds check
    }
}

Codegen diff: https://www.diffchecker.com/7iCHeDLt/

Seems like this transformation produces nice SPMI diffs.

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 20, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@EgorBo EgorBo marked this pull request as ready for review April 20, 2025 17:02
Copilot AI review requested due to automatic review settings April 20, 2025 17:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements an optimization to fold the expression new T[size].Length into the original size argument, reducing unnecessary bounds checks and streamlining array length calculations.

  • Introduces a new folding case for ARR_LENGTH with new array expressions.
  • Handles different type cases (TYP_LONG and TYP_INT) to extract the original size.

@EgorBo
Copy link
Member Author

EgorBo commented Apr 20, 2025

PTAL @AndyAyersMS @jakobbotsch @dotnet/jit-contrib simple opt with diffs.

@EgorBo EgorBo requested a review from AndyAyersMS April 20, 2025 17:04
@neon-sunset

This comment was marked as resolved.

@EgorBo EgorBo merged commit 8af737a into dotnet:main Apr 22, 2025
113 checks passed
@EgorBo EgorBo deleted the fold-arr-len-pattern branch April 22, 2025 11:33
@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: Redundant bounds check for when loopUpperBound==arrayLength

3 participants